<template>
  <el-dialog
    :title="state.title"
    @close="handleClose"
    width="800"
    v-model="state.visible"
    :before-close="beforeClose"
    :loading="state.loading"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    append-to-body
    destroy-on-close
  >
    <div>
      <yt-crud
        v-bind="options"
        ref="crudRef"
        @onLoad="getData"
        :fun-props="{
          exportBtn: false,
          delBtn: false,
          addBtn: false,
          hide: true,
        }"
        :table-props="{
        selection: false,
          delBtn: false,
          pageHide: true,
          viewBtn: false,
          editBtn: false,
          menu: false,
        }"
        :form-props="{
          loading: state.form.loading,
          width: 500,
        }"
        :loading="state.loading"
        :total="state.total"
        v-model:page="state.page"
        v-model:query="state.query"
      >
        <template #amount="scope:any">
          <div v-if="scope.row.type === 'Normal'">{{scope.row.amount}}</div>
          <div v-else>{{scope.row.discountAmount}}</div>
        </template>

        <template #type="scope:any">
          <DictTag :options="OrderSettleTypeEnum" :value="scope.row.type" />
        </template>

        <template #discountAmount="scope:any">
          <div v-if="scope.row.discountType">
            <span>{{scope.row.discountAmount}} </span>
            <span> ({{ t('enum.discountTypeEnum.' + lowerFirst(scope.row.discountType))}})</span>
          </div>
          <div v-else>{{scope.row.discountAmount}}</div>
        </template>
      </yt-crud>
    </div>

    <template #footer>
      <el-button @click="handleClose" :loading="state.loading">{{t('crud.closeText')}}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>

import YtCrud from '@/components/common/yt-crud.vue'
import {IColumn} from '@/components/common/types/tableCommon'
import {orderSettleList } from '@/api/business/orderSettle'
import {useI18n} from 'vue-i18n'
import {ComponentInternalInstance} from 'vue'
import { lowerFirst } from '@/utils/ruoyi'

const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { OrderSettleTypeEnum } = toRefs<any>(proxy?.useEnum('order_settle_type_enum'))

const { t } = useI18n()

const state = reactive({
  visible: false,
  loading: false,
  recharge: {},
  form: {
    loading: false,
  },
  title: t('business.order.action.settleView'),
  page: {
    pageSize: 30,
    pageNum: 1,
  },
  total: 0,
  query: {},
})

const ordersSettle = ref<any>({})

const column = ref<IColumn[]>(
  [
    {
      label: t('business.orderSettle.type'),
      key: 'type',
      slot: true,
    },
    {
      label: t('business.orderSettle.amount'),
      key: 'amount',
      slot: false,
    },
    {
      label: t('business.orderSettle.settledAmount'),
      key: 'settledAmount',
      slot: false,
    },
    {
      label: t('business.orderSettle.discountAmount'),
      key: 'discountAmount',
      slot: true,
    },
    {
      label: t('crud.table.column.createTime'),
      key: 'createTime',
      tableWidth: 180,
      sortable: true,
      type: 'date',
      formHide: true,
    },
  ]
)

const openDialog = (row?: any) => {
  ordersSettle.value = row
  state.visible = true
}

const handleClose = () => {
  state.visible = false
}

const beforeClose = (done: () => void) => {
  done()
}
defineExpose({
  openDialog,
})

const crudRef = ref()
const data = ref<any[]>([])
const getData = () => {
  state.loading = true
  orderSettleList({
    ...state.page,
    ...state.query,
    ...{ orderId: ordersSettle.value.id}
  })
    .then((res) => {
      data.value = res.data.rows
      state.total = res.data.total
    })
    .finally(() => {
      state.loading = false
    })
}


const options = reactive({
  ref: 'crudRef',
  data,
  column,
})
</script>
